<template>
  <t-dialog :visible.sync="visible"
  :header="header"
  confirmBtn="提交"
  :confirmLoading="confirmLoading"
  :onConfirm="submit"
  :onClose="closeDialog"
  :closeOnOverlayClick="false"
  placement="center"
  width="60%">
      <div style="height: 60vh; width: 98%" v-if="!dataLoading">
          <t-form ref="form" :data="formData" :rules="rules" :colon="true">
          <t-row :gutter="[14,25]">
            <t-col :span="3">
                <t-form-item label="人才" name="pid">
                    <t-select clearable :options="options" v-model="formData.pid"></t-select>
                </t-form-item>
              </t-col>
              <t-col :span="3">
                  <t-form-item label="时间" name="educationDate">
                      <t-date-picker clearable v-model="formData.educationDate"></t-date-picker>
                  </t-form-item>
              </t-col>
              <t-col :span="6">
                  <t-form-item label="地点" name="address">
                      <t-input clearable v-model="formData.address"></t-input>
                  </t-form-item>
              </t-col>
              <t-col :span="12">
                <t-form-item label="内容" name="content">
                  <t-textarea :autosize="{minRows: 20}" v-model="formData.content"></t-textarea>
                </t-form-item>
              </t-col>
              <t-col :span="12">
                <t-form-item label="相关附件" name="files">
                    <file-upload v-model="fileIds"
                                 :fileList="fileLists"
                                 :multiple="true">
                      <t-button variant="outline">
                        <t-icon name="cloud-upload" slot="icon"/>
                        点击上传
                      </t-button>
                    </file-upload>
                </t-form-item>
              </t-col>
          </t-row>
      </t-form>
      </div>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"


export default {
  name: "unit-add-edit",
  dicts: ['kpzh_manage_org_code', 'kpzh_national_economy_code', 'kpzh_unit_level', 'kpzh_source_funds'],
  components: {
    FileUpload
  },
  props:{
    objId: {
      type: [String, Number],
      default: '',
    },
    header: {
      type: String,
      default: '新增'
    },
    row: {
      type: Object,
      default: () => ({})
    },
    isUpdate: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: true,
      options: [],
      formData: {
        pid: '',
        educationDate: '',
        address: '',
        content: '',
        
      },
      fileIds: [],
      fileLists: [],
      dataLoading: false,
      locationOptions: [],
      menuOptions: [],
      confirmLoading: false,
      rules: {
        name: [
          { required: true, message: "单位详细名称不能为空" }
        ],
        
      },
      queryParams: {
        pageNum: 1,
        pageSize: 100000,
        name: "",
        status: "",
        dateRange: []
      },
      treeProps: {
        keys: {
          value: 'id'
        },
      }
    }
  },
  created() {
    Object.assign(this.formData, this.row)
    this.getTreeSelect();
    this.getLocation();
    this.getTalents();
  },
  methods: {
    closeDialog() {
      this.$emit("update:visible", false);
    },
    treeChange(value, context) {
      this.formData.orgStats = context.data?.id
    },
    treeClear() {
      this.formData.orgStats = null
    },
    submit() {
      if (this.formData.orgStats === null) {
        return 
      }
      this.confirmLoading = true
      this.$api.kjrc.talentsEducation.add({
        ...this.formData,
        sysAttMainVo: {
          sysAttMainIds: this.fileIds
        }
      }).then(res => {
        this.confirmLoading = false
        this.visible = false
        this.$emit("update:visible", false);
        this.$emit("reload")
      }).catch((e) => {
        this.$message.error(e.toString());
        this.confirmLoading = false
      });
    },
    getTreeSelect() {
      this.$api.kpzh.unit.treeSelect().then(response => {
        this.menuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getLocation(){
      return this.$api.system.location.treeselect().then(response => {
        this.locationOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getTalents() {
      this.dataLoading = true;
      this.$api.kjrc.talents.myList(this.queryParams).then(response => {
        response.rows.forEach(item => {
          this.options.push({key: item.id, label: item.name, value: item.id})
        })
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
  },
}

</script>

<style scoped>

</style>